package com.example.his.api.front.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.bean.BeanUtil;
import com.example.his.api.common.R;
import com.example.his.api.config.sa_token.StpCustomerUtil;
import com.example.his.api.front.controller.form.CreatePaymentForm;
import com.example.his.api.front.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Map;

/**
 * @author huixh
 */
@Slf4j
@RestController
@RequestMapping("/front/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @PostMapping("/createPayment")
    @SaCheckLogin(type = StpCustomerUtil.TYPE)
    public R createPayment(@RequestBody @Valid CreatePaymentForm form){
        int customerId = StpCustomerUtil.getLoginIdAsInt();
        Map<String, Object> map = BeanUtil.beanToMap(form);
        map.put("customerId",customerId);
        Map<String,Object> result = orderService.createPayment(map);
        if (result == null) {
            return R.ok().put("illegal",true);
        } else {
            return R.ok().put("illegal",false).put("result",result);
        }
    }
}
